package group2.core.entities;

import group2.core.UserLevel;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

/**
 * This class contains the camelUser information. It contains all contributions
 * made by the user and the identifying data.
 * 
 * @author Christian
 */
@Entity
public class CamelUser implements Serializable {

    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @Column(nullable = false, length = 50)
    private String userName;
    
    @Column(nullable = false, length = 50)
    private String password;
    
    @Column(length = 50)
    private String firstName;
    
    @Column(length = 50)
    private String lastName;
    
    @Column(nullable = false, length = 50,unique=true)
    private String email;
    
    @Enumerated(EnumType.STRING)
    private UserLevel userLevel;
    
    @OneToMany(mappedBy = "commentAuthor", cascade = CascadeType.PERSIST, orphanRemoval=true)
    private List<Comment> comments;
    
    @OneToMany(mappedBy = "postAuthor", cascade = CascadeType.PERSIST, orphanRemoval = true)
    private List<Post> posts;

    public CamelUser() {
    }

    public CamelUser(long id, String userName, String password, String firstName, String lastName, String email) {
        this.id = id;
        this.userName = userName;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.posts = new ArrayList<Post>();
        this.comments = new ArrayList<Comment>();
        this.userLevel = UserLevel.USER;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UserLevel getUserLevel() {
        return userLevel;
    }

    public void setUserLevel(UserLevel userLevel) {
        this.userLevel = userLevel;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void addComment(Comment c) {
        comments.add(c);
    }

    public void removeComment(Comment c) {
        comments.remove(c);
    }

    public List<Post> getPosts() {
        return posts;
    }

    public void addPost(Post p) {
        posts.add(p);
    }

    public void removePost(Post p) {
        posts.remove(p);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }


    /**
     * 
     * @param object
     * @return false if objects differ or both are null.
     */
    @Override
    public boolean equals(Object object) {
        //This method will return false is this==null && other==null
        if (object==null || !(object instanceof CamelUser)) {
            return false;
        }
        
        CamelUser other = (CamelUser) object;
        if(this.id==null && other.id==null){
            return false;
        }

        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "group2.core.CamelUser[ id=" + id + " ]";
    }
}
